using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OsdmSoftware.CommonLibrary;

namespace OsdmSoftware.PocketPicture
{
    public class SmallPaletteControl: ToolPanel, IDropDownControl
    {
        public SmallPaletteControl()
        {
        }

        public void SetColorNumber(int colorNumber)
        {
            if (ChildElements.Count > colorNumber)
            {
                while (ChildElements.Count > colorNumber)
                {
                    long mostOldAccessed = Int64.MaxValue;
                    ColorElement mostOldAccessedElement = null;
                    foreach (ToolPanelElement tpe in ChildElements)
                    {
                        if (tpe is ColorElement)
                        {
                            ColorElement ce = (ColorElement) tpe;
                            if ((ce.LastAccessed <= mostOldAccessed) && !ce.IsPushed)
                            {
                                mostOldAccessedElement = ce;
                                mostOldAccessed = ce.LastAccessed;
                            }
                        }
                    }

                    ChildElements.Remove(mostOldAccessedElement);
                }
            }
            else
            {
                Color[] colors = { Color.Black, Color.Red, Color.Orange, Color.Yellow, Color.Green,
                  Color.LightBlue, Color.Blue, Color.Fuchsia, Color.Gray, Color.White,
                  Color.Brown, Color.Cyan, Color.DarkViolet, Color.GreenYellow,
                  Color.FromArgb(200, 150, 63), // Light brown
                  Color.Goldenrod, Color.DeepPink, /*Color.Honeydew, */ Color.Bisque, Color.Indigo, Color.Ivory,
                  Color.Khaki, Color.Lavender, Color.LemonChiffon, Color.Aquamarine, Color.Azure,
                  Color.LightGreen, Color.Crimson, Color.Gold, Color.DeepSkyBlue, Color.Beige, Color.Coral};
                int colorIndex = 0;
                int add = colorNumber - ChildElements.Count;
                for (int i = 0; i < add; i++)
                {
                    if (colorIndex >= colors.Length)
                    {
                        ChildElements.Add(new ColorElement(Color.White));
                    }
                    else
                    {
                        bool found = true;
                        while (found)
                        {
                            found = false;
                            foreach (ToolPanelElement tpe in ChildElements)
                            {
                                if (tpe is ColorElement)
                                {
                                    ColorElement ce = (ColorElement) tpe;
                                    if (Utility.RGBEqual(ce.Color, colors[colorIndex]))
                                    {
                                        found = true;
                                        break;
                                    }
                                }
                            }

                            if (found)
                                colorIndex++;
                            if (colorIndex >= colors.Length)
                                break;
                        }

                        if (colorIndex >= colors.Length)
                        {
                            ChildElements.Add(new ColorElement(Color.White));
                        }
                        else
                        {
                            ChildElements.Add(new ColorElement(colors[colorIndex]));
                        }
                    }
                }
            }
        }

        public void ColorChanged(Color newColor)
        {
            ColorElement mostOldAccessedElement = null;
            ColorElement thisColor = null;
            long mostOldAccessed = Int64.MaxValue;
            foreach (ToolPanelElement tpe in ChildElements)
            {
                if (tpe is ColorElement)
                {
                    ColorElement ce = (ColorElement) tpe;
                    if (Utility.RGBEqual(ce.Color, newColor))
                    {
                        thisColor = ce;
                    }

                    if (ce.LastAccessed < mostOldAccessed)
                    {
                        mostOldAccessedElement = ce;
                        mostOldAccessed = ce.LastAccessed;
                    }

                    if (ce.IsPushed)
                    {
                        ce.IsPushed = false;
                        ce.Refresh();
                    }
                }
            }

            if (thisColor != null)
            {
                thisColor.IsPushed = true;
                thisColor.LastAccessed = DateTime.Now.Ticks;
                thisColor.Refresh();
            }
            else if (mostOldAccessedElement != null)
            {
                mostOldAccessedElement.Color = newColor;
                mostOldAccessedElement.IsPushed = true;
                mostOldAccessedElement.LastAccessed = DateTime.Now.Ticks;
                mostOldAccessedElement.Refresh();
            }
        }
    }

    public class ColorElement: ToolPanelElement
    {
        Color color;

        public Color Color { get { return color; } set { color = value; } }

        long lastAccessed;

        public long LastAccessed { get { return lastAccessed; } set { lastAccessed = value; } }

        public ColorElement(Color color)
        {
            this.color = color;
        }

        //public Rectangle Bounds { get { return new Rectangle(location.X - panel.SpanSize / 2, location.Y - panel.SpanSize / 2, size.Width + panel.SpanSize / 2 * 2, size.Height + panel.SpanSize / 2 * 2); } }
        //public override Rectangle MaxBounds { get { return Bounds; } }
        public override Rectangle MaxBounds
        {
            get
            {
                int indent = Panel.SpanSize / 2 + 1;

                return new Rectangle(Location.X - indent, Location.Y - indent, Size.Width + indent * 2, Size.Height + indent * 2);
            }
        }

        public override Size ComputeSize()
        {
            Size = new Size(Panel.ImageSize, Panel.ImageSize);
            return Size;
        }

        public override void Paint(Graphics gr, int x, int y)
        {
            if (Panel == null)
                return;
            Color backColor = Panel.BackColor;

            /*if (Panel.ShowGrid)
            {
                gr.DrawRectangle(new Pen(Panel.ForeColor, 1), x - Panel.SpanSize / 2, y - Panel.SpanSize / 2,
                    Size.Width + Panel.SpanSize, Size.Height + Panel.SpanSize);
            }*/

            if (IsPushed)
            {
                int indent = Panel.SpanSize / 2 + 1;
                gr.FillRectangle(new SolidBrush(Panel.SelectionColor), x - indent, y - indent,
                    Size.Width + indent * 2 - 1, Size.Height + indent * 2 - 1);
                gr.DrawRectangle(new Pen(Panel.ForeColor, 1), x - indent, y - indent,
                    Size.Width + indent * 2 - 1, Size.Height + indent * 2 - 1);
            }

            gr.FillRectangle(new SolidBrush(color), new Rectangle(x, y, Panel.ImageSize - 1, Panel.ImageSize - 1));
            gr.DrawRectangle(new Pen(Panel.ForeColor, 1), new Rectangle(x, y, Panel.ImageSize - 1, Panel.ImageSize - 1));
        }
    }
}
